#!/bin/sh
# invoke ntp server based on nvrams vars ntp_server and time_zone
if [ -f "/usr/sbin/ntpclient" ] || \
	[ -f "/usr/bin/ntpclient" ] || \
	[ -f "/sbin/ntpclient" ] || \
	[ -f "/bin/ntpclient" ]; then	
	
	# create symlink to /tmp/TZ if /etc/TZ doesn't exist
	# todo: -e | -f | -d didn't seem to work here, so I used find
	if [ -z $(find "/etc/TZ") ]; then 
		ln -s /tmp/TZ /etc/TZ
	fi
	
	# eJunky: set timezone
	TZ=$(nvram get time_zone | cut -f 2)
	[ "$TZ" ] && echo $TZ > /etc/TZ
		
	# get server, use pool.ntp.org if not set
	NTP_SERVER=$(nvram get ntp_server)
	if [ -z "$NTP_SERVER" ]; then
		NTP_SERVER="pool.ntp.org"
		nvram set "ntp_server=$NTP_SERVER"
	fi
	
	# launch ntpclient
	killall ntpclient
	ntpclient -i 86400 -h $NTP_SERVER &
else
	logger "$0: ntpclient not found to be installed."	
fi
